import { reactive, ref } from "vue"
import store from '@/store'
import { storage } from '@/utils/Storage'
import { CODEENUM } from '@/config'
import { selectAll } from '@/api/tag'

export default function useTag() {

  const tagList = ref([])

  const getTagList = async () => {

    // 如果本地已有tag缓存数据，就直接return掉，不重新请求
    const tags = getTags()
    if (tags && tags.length) return
    // 取出所有状态为 1 的tag
    const { data, code } = await selectAll({ status: 1 })
    if (code !== CODEENUM.GD_SUCCESS) return
    setTags(data)
  }

  const getTags = () => {
    return store.getters.tagList || storage.get('tagList')
  }

  const setTags = (data) => {
    tagList.value = data
    store.commit('CHANGE_SETTING', { key: 'tagList', value: data })
    storage.set('tagList', data)
  }


  return {
    tagList,
    getTagList,
    getTags,
  }
}